=head
@pos=(41500001,42500002,43600002,43900002);
@event=('<DUP:TANDEM>','<DUP:TANDEM>','<DEL>','<DEL>');
@num=(2,7,1,1);
@eventsize=(50000,50000,50000,50000);
=cut
if(@ARGV != 6)
{
	die "perl perl_modify_bed_insert_delete.pl pos1,pos2 event_type,event_type num_event,num_event eventsize,eventsize\n";
}
chomp($ARGV[0]);
chomp($ARGV[1]);
chomp($ARGV[2]);
chomp($ARGV[3]);
@pos=split(',',$ARGV[0]);
@event=split(',',$ARGV[1]);
@num=split(',',$ARGV[2]);
@eventsize=split(',',$ARGV[3]);
#die "@pos\n@event\n@num\n@eventsize\n";
chomp($ARGV[4]);
chomp($ARGV[5]);
#open(BUFF,"../refdir/SureSelect_All_Exon_V4+UTRs_with_annotation_hg19_chr22_q13_2.bed") or die "no file found S03723424_Probes_chr22_q13_2.bed\n";
#open(WRBUFF,">SureSelect_All_Exon_V4+UTRs_with_annotation_hg19_chr22_q13_2.bed") or die "no file found $ARGV[4]\n";
open(BUFF,$ARGV[4]) or die "no file found $ARGV[4]\n";
open(WRBUFF,">$ARGV[5]") or die "not able to write the file $ARGV[5]\n";
#$k=0;
#while(<BUFF>)
#{
#	chomp($_);
#	$k++;
#	print WRBUFF "$_\tm$k\n";
#}
#open(BUFF,"SureSelect_All_Exon_V4+UTRs_with_annotation_hg19_chr22_q13_2.bed");
#open(WRBUFF,">SureSelect_All_Exon_V4+UTRs_with_annotation_hg19_chr22_q13_2_modified.bed");
#settign increment to zero and used as tracking tool
$increment=0;
#reading the initial line
$line=<BUFF>;
chomp($line);
@array=split("\t",$line);
$start=$array[1];
$stop=$array[2];
$index=$array[3];	
my @temp_event;
#parsing through different sorted events
for($i=0;$i<@event;$i++)
{
	#intializing enent start and stop
	$event_start=$pos[$i];
	$event_stop=$pos[$i]+$eventsize[$i];
	#resetingt he temp_event array at each event
	undef(@temp_event);
	#writing probes before event
	while($start < $event_start)
	{
		$start = $start+$increment;
                $stop = $stop+$increment;
                print  WRBUFF "$array[0]\t$start\t$stop\t$index\n";
		#if($m ==1)
		#{
		#	die "$array[0]\t$start\t$stop\n";
		#}
		$line=<BUFF>;
		chomp($line);
		@array=split("\t",$line);
		$start=$array[1];
		$stop=$array[2];
		$index=$array[3];
	}
	#storing event probes in the temp_event array
	while($start <= $event_stop)
	{
		#print "$line\n";
		push(@temp_event,$line);
		$line=<BUFF>;
		chomp($line);
		@array=split("\t",$line);
		$start=$array[1];
		$stop=$array[2];
		$index=$array[3];
	}
	#die;
	#print @temp_event." $event[$i] $num[$i]\n";
	#if the event is CNV 
	if($event[$i] =~ /DUP/)
	{	
		$m=1;
		#remove one of the other event
		#$num[$i]--;
		#repeat number of times each CNV inserted
		for($j=0;$j<=$num[$i];$j++)
		{
			for($z=0;$z<@temp_event;$z++)
			{
     				@array=split("\t",$temp_event[$z]);
				$start1=$array[1]+$increment;
				$stop1 = $array[2]+$increment;
				$index1=$array[3];
				print WRBUFF "$array[0]\t$start1\t$stop1\t$index1\n";
				#print "$array[0]\t$start\t$stop\n";
			}	
			#print "\n\n\n";
			$increment=$increment+$eventsize[$i];
		}
		$increment=$increment-$eventsize[$i];
	 	#die "done\n";
	}
	#if event is deletin dont print event
	elsif($event[$i] =~ m/DEL/ )
	{
		if( $num[$i] > 0)
		{
		#print "sucess\n";
		$increment=$increment-$eventsize[$i];
		}
		else
		{
			for($z=0;$z<@temp_event;$z++)
                        {
                                @array=split("\t",$temp_event[$z]);
                                $start1=$array[1]+$increment;
                                $stop1 = $array[2]+$increment;
                                $index1=$array[3];
                                print WRBUFF "$array[0]\t$start1\t$stop1\t$index1\n";
                                #print "$array[0]\t$start\t$stop\n";
                        }
		}
	}
	else
	{
		die "what nonsense event is this $event[$i]\n";
	}	
}
print WRBUFF "$line\n";
while($line=<BUFF>)
{
	chomp($line);
     @array=split("\t",$line);
     $start = $array[1]+$increment;
     $stop = $array[2]+$increment;
     $index=$array[3];
     print WRBUFF "$array[0]\t$start\t$stop\t$index\n";
}

